﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArPanel : UIBase {

    RawImage rawImage;
    Animation scanning_img;
    Button startBtn;
    Button stopBtn;
    GameObject scanning_img_obj;


    void Awake () {
        rawImage = transform.Find("RawImage").GetComponent<RawImage>();
        startBtn = transform.Find("StartBtn").GetComponent<Button>();
        stopBtn = transform.Find("StopBtn").GetComponent<Button>();
        scanning_img = transform.Find("scanning_img").GetComponent<Animation>();
        scanning_img_obj = transform.Find("scanning_img").gameObject;
        startBtn.onClick.AddListener(OnStartClick);
        stopBtn.onClick.AddListener(OnStopClick);

        StartWebCamera();
    }
	
	void OnStartClick ()
    {
        //startBtn.gameObject.SetActive(false);
        //stopBtn.gameObject.SetActive(true);

        //StartCoroutine(CheckFace());
        CheckFace();

        //登录
        //Island.PBAccount pb = new Island.PBAccount();
        //pb.id = Util.UUID;
        //pb.sex = Random.Range(0, 2);
        //NetworkManager.Instance.Send(Protocol.ReqLogin, pb);


    }

    void OnStopClick()
    {
        startBtn.gameObject.SetActive(true);
        stopBtn.gameObject.SetActive(false);

        rawImage.gameObject.SetActive(false);
        WebCamTool.Instance.StopWebCamera();

        //推出应用
        Application.Quit();
    }

    public override void Init(params object[] args) {
        foreach (var item in ActorManager.Instance.Actors)
        {
            item.Value.gameObject.SetActive(true);
        }
    }
    public override void OnClose(params object[] args) {
        foreach (var item in ActorManager.Instance.Actors)
        {
            item.Value.gameObject.SetActive(false);
        }
    }


    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                var actor = hit.collider.GetComponent<Actor>();
                if (actor != null)
                {
                    UIManager.Instance.Open<DetailPanel>(actor.account);
                    UIManager.Instance.Close<ArPanel>();
                }
            }
        }
    }

    /// <summary>
    /// 开启WebCamare，拍摄画面在RawIamge显示
    /// </summary>
    void StartWebCamera()
    {
        rawImage.gameObject.SetActive(true);
        WebCamTool.Instance.StartWebCamera(rawImage);

        //StartCoroutine(CheckFace());
    }

    //开启协程监听是否扫描到人体，2s一次
    //IEnumerator CheckFace()
    //{
    //    //while (true)
    //    //{
    //        //yield return new WaitForSeconds(2);            
    //    //}
    //}
    void CheckFace()
    {
        if (rawImage.isActiveAndEnabled && rawImage.texture != null)
        {
            var bytes = WebCamTool.Instance.GetBytes(rawImage);
            BaiduFaceAI.Instance.BodyAnalysis(bytes, (isHuman) => {
                if (isHuman)
                {
                    Debug.Log("检测到人体");
                    UIManager.Instance.Open<UseInfoPanel>();
                    scanning_img_obj.SetActive(false);
                    scanning_img.Stop();
                }
            });
        }
    }
}
